# coding:utf-8
import time
import tkinter as tk
from datetime import datetime

import ttkbootstrap as tkBoot

from util.mysql_util import add_classes
from util.ui_util import show_center

DATE_TIME_FORMAT = "%Y-%m-%d %H:%M:%S"


class AddClassBox(tkBoot.Toplevel):
    def __init__(self, parent):
        super().__init__(parent)
        self.parent = parent
        self.className = tk.StringVar()

    def initFrame(self):
        width = int(self.winfo_screenwidth()/6)
        height = int(self.winfo_screenheight()/6)
        self.title("新建班级")
        show_center(self, width, height)
        self.resizable(False, False)

        nameFrame = tkBoot.Frame(self)
        tkBoot.Label(nameFrame, text="班级名称").pack(side=tk.LEFT, padx=(0, 10))
        tkBoot.Entry(nameFrame, textvariable=self.className).pack(side=tk.RIGHT)
        nameFrame.pack(pady=(20, 0))

        btnFrame = tkBoot.Frame(self)
        tkBoot.Button(btnFrame, text="确认", command=self.confirm).pack(side=tk.LEFT, padx=(0, 10))
        tkBoot.Button(btnFrame, text="取消", command=self.cancel).pack(side=tk.RIGHT)
        btnFrame.pack(side=tk.BOTTOM, pady=(0, 20))

    def confirm(self):
        now = datetime.now().strftime(DATE_TIME_FORMAT)
        rows = [(self.className.get(), now, now)]
        add_classes(rows[0])
        self.parent.refresh_table_data()
        time.sleep(0.1)
        self.destroy()

    def cancel(self):
        self.destroy()
